// components/PFEP_components/find_pfep_standard/find_pfep_standard.js
const mybehavior = require("../../../behaviors/requests")
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [mybehavior],
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    company: '',
    pfep_username: "",
    material_num: '',
    is_loading: false,
    process: '逆向物流',
    processes: [
      '包装',
      '拣选发货',
      '运输', '入厂', '卸货', '收货', '检验', '入库仓储',
      '分拣出库', '厂内配送', '车间收货', '线边仓暂存', '分拣', '配送上线', '岗位使用', '逆向物流'
    ],
    showPicker: false,
    supplies: '',
    showPicker1: false,
    supplier_code: [],
    showPicker2: false,
    company_list: [],
    list: [],
    reverse_list: [],
    reverse_target: {},
    otherInfo: {},
    base_url: 'https://logisticsengineering.gree.com',
    otherProcess: [],
    showProcess: '逆向物流',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSubmit() {
      this.setData({
        is_loading: true
      })
      let url = this.data.base_url + "/pfep_api/phone_manage/phone_search_standard"
      let data = {
        material_num: this.data.material_num,
        process: this.data.process,
        supply_no: this.data.supplies,
        company: this.data.company
      }
      const self = this;
      this.post(url, data).then(res => {
        self.setData({
          is_loading: false
        })
        if (res.code == 20000) {
          var data = res.data
          if (self.data.showProcess == '包装') {
            self.setData({
              list: [...data.content['包装容器/辅材']],
              otherInfo: {
                ...data.content['包装数据']
              }
            })
          } else if (self.process == "逆向物流") {
            // 逆向物流环节相关数据整理
            self.setData({
              otherInfo:{
                ...data.content['清退标准']
              },
              reverse_target:{
                ...data.content['清退容器信息']
              }
            })
            let container = data.content['打包用容器']
            container.id = "temp1000001";
            let reverse_list = [...self.reverse_list];
            reverse_list.push(container);
            reverse_list.push(...data.content['打包用辅材']);
            self.setData({
              reverse_list:reverse_list
            })
          } else {
            const temp = [];
            for(var index in data.content){
                 const obj = {};
                 obj["index"] = index;
                 obj["content"] = data.content[index]["内容"];
                 obj["sort_index"] = data.content[index]["index"];
                 temp.push(obj);
            };
            temp.sort((a,b)=>{
              a.sort_index<=b.sort_index;
            });
            self.setData({
              otherProcess:temp
            })
          }
        } else {
          Toast.fail(res.msg);
        }
      }).catch(_ => {
        console.log(_);
        self.setData({
          is_loading: false
        })
        wx.showToast({
          title: '出错了',
          icon: 'error'
        })
      })
      // request({
      //   url: this.base_url + "/pfep_api/phone_manage/phone_search_standard",
      //   method: 'post',
      //   params: {
      //     material_num: this.material_num,
      //     process: this.process,
      //     supply_no: this.supplies,
      //     company: this.company
      //   }
      // }).then(res => {
      //   this.is_loading = false;
      //   if (res.code == 20000) {
      //     var data = res.data;
      //     if (this.process == '包装') {
      //       this.list = [...data.content['包装容器/辅材']]
      //       this.otherInfo = {
      //         ...data.content['包装数据']
      //       }
      //     } else if (this.process == "逆向物流") {
      //       // 逆向物流环节相关数据整理
      //       this.otherInfo = {
      //         ...data.content['清退标准']
      //       }
      //       this.reverse_target = {
      //         ...data.content['清退容器信息']
      //       }
      //       let container = data.content['打包用容器']
      //       container.id = "temp1000001"
      //       this.reverse_list.push(container)
      //       this.reverse_list.push(...data.content['打包用辅材'])
      //     } else {
      //       this.otherProcess = {
      //         ...data.content
      //       };
      //       let otherProcessKeys = Object.keys(this.otherProcess);
      //       // 排序专用
      //       for (let i = 1; i <= otherProcessKeys.length; i++) {
      //         this.otherProcessOrders.push(i)
      //       }
      //     }
      //   } else {
      //     Toast.fail(res.msg);
      //   }
      // }).catch(err => {
      //   Toast.fail(err.msg)
      //   this.is_loading = false;
      // })
    },
    openShowPicker2() {
      this.setData({
        showPicker2: true,
      })
    },
    closeShowPicker2() {
      this.setData({
        showPicker2: false
      })
    },
    onConfirm2(ev) {
      const {
        value
      } = ev.detail;
      this.setData({
        company: value,
        showPicker2: false
      })
    },
    wechat_scan() {

    },
    openShowPicker() {
      this.setData({
        showPicker: true
      })
    },
    onConfirm(ev) {
      const {
        value
      } = ev.detail;
      this.setData({
        process: value,
        showProcess: value,
        showPicker: false
      })
    },
    closeShowPicker() {
      this.setData({
        showPicker: false
      })
    },
    query_company_list() {
      var url = this.data.base_url + '/pfep_api/online_config_manage/query_company_list'
      var data = {}
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          this.setData({
            company_list: [...res.data]
          })
        } else {
          this.setData({
            company_list: []
          })
        }
      }).catch(err => {
        this.setData({
          company_list: []
        })
      })
    }
  },
  lifetimes: {
    attached: function () {
      this.query_company_list();
    },
    ready: function () {},
    detached: function () {},
  }
})